*****************************************************************************
* 																			*
*	Campaign Panel Book Do-File Chapter 2 - Exposure by Involvement			*		
*																			*
*****************************************************************************

/*
calculate linear regression
calculate average regression coefficients over time
standard deviations of the individual regression coefficients from wave mean

Regressions for campaign contact only from wave 2 to wave 6; therefore
drop wave 7 from !missing()-statement
*/

*Adding W2 starters from 2009
gen a_indiff12_h = a_indiff1
replace a_indiff12_h = a_indiff2 if a_w1==0
gen b_indiff12_h = b_indiff1



* Regressionmodels

foreach year in a b {
foreach wave of numlist 2/7 {
foreach var in _oeff _privat _paper _bild _paper_and _magazine _polinter _talk {
reg `year'`var'`wave' `year'_polint2 `year'_pidno1 `year'_indiff12_h ///
	if !missing(`year'`var'2, `year'`var'3, `year'`var'4, `year'`var'5, ///
	`year'`var'6, `year'`var'7) ///
	[pw=`year'_weight]

mat list e(b)

gen `year'`var'`wave'_polint2=_b[`year'_polint2] if e(sample)
gen `year'`var'`wave'_pidno1=_b[`year'_pidno1] if e(sample)
gen `year'`var'`wave'_indiff12_h=_b[`year'_indiff12_h] if e(sample)
gen `year'`var'`wave'_cons=_b[_cons] if e(sample)
}
}
}

* Regressionmodel für campaign contacts (only waves 2-6)

foreach year in a b {
foreach wave of numlist 2/6 {
foreach var in _countcontact {
qui reg `year'`var'`wave' `year'_polint2 `year'_pidno1 `year'_indiff12_h ///
	if !missing(`year'`var'2, `year'`var'3, `year'`var'4, `year'`var'5, ///
	`year'`var'6) ///
	[pw=`year'_weight]

mat list e(b)

gen `year'`var'`wave'_polint2=_b[`year'_polint2] if e(sample)
gen `year'`var'`wave'_pidno1=_b[`year'_pidno1] if e(sample)
gen `year'`var'`wave'_indiff12_h=_b[`year'_indiff12_h] if e(sample)
gen `year'`var'`wave'_cons=_b[_cons] if e(sample)
}
}
}


* Aggregation of wave means

foreach year in a b {
foreach var in _oeff _privat _paper _bild _paper_and _magazine _polinter _talk {
foreach coef in _polint2 _pidno1 _indiff12_h _cons {
gen `year'`var'`coef'=(`year'`var'2`coef'+`year'`var'3`coef'+`year'`var'4`coef'+`year'`var'5`coef' ///
						+`year'`var'6`coef'+`year'`var'7`coef')/6

}
}
}

fre a_oeff_pidno1

*Aggregation of wave means campaign contact

foreach year in a b {
foreach var in _countcontact {
foreach coef in _polint2 _pidno1 _indiff12_h _cons {
gen `year'`var'`coef'=(`year'`var'2`coef'+`year'`var'3`coef'+`year'`var'4`coef'+`year'`var'5`coef' ///
						+`year'`var'6`coef')/5
						
}
}
}


* Aggregation of standard deviations
 
foreach year in a b {
foreach var in _oeff _privat _paper _bild _paper_and _magazine _polinter _talk {
foreach coef in _polint2 _pidno1 _indiff12_h _cons {

gen `year'`var'sd`coef' = sqrt((((`year'`var'2`coef'-`year'`var'`coef')^2)+((`year'`var'3`coef'-`year'`var'`coef')^2)+((`year'`var'4`coef'-`year'`var'`coef')^2) ///
	+((`year'`var'5`coef'-`year'`var'`coef')^2)+ ///
	((`year'`var'6`coef'-`year'`var'`coef')^2)+ ///
	((`year'`var'7`coef'-`year'`var'`coef')^2))/6)
			
}
}
}


* Aggregation standard deviations campaign contact
 
foreach year in a b {
foreach var in _countcontact {
foreach coef in _polint2 _pidno1 _indiff12_h _cons {

gen `year'`var'sd`coef' = sqrt((((`year'`var'2`coef'-`year'`var'`coef')^2)+((`year'`var'3`coef'-`year'`var'`coef')^2)+((`year'`var'4`coef'-`year'`var'`coef')^2) ///
	+((`year'`var'5`coef'-`year'`var'`coef')^2)+ ///
	((`year'`var'6`coef'-`year'`var'`coef')^2))/5)
			
}
}
}


* Table
estimates clear
foreach year in a b {
foreach var in _oeff _privat _paper _bild _paper_and _magazine _polinter _talk _countcontact {
foreach coef in _polint2 _pidno1 _indiff12_h _cons {

mean `year'`var'`coef' 
estimates store `year'`var'`coef'

sum `year'`var'sd`coef'
estadd scalar `year'`var'_sd`coef' = r(mean) , replace

}
}
}

*****************************************************
***Table 5
*****************************************************


* 2009

esttab a_oeff_polint2 a_privat_polint2 a_paper_polint2 a_bild_polint2 a_paper_and_polint2 a_magazine_polint2 a_polinter_polint2 a_talk_polint2 a_countcontact_polint2 ///
	using "TablesII/nice table.rtf", replace ///
	b(2) not nonumb stats(a_oeff_sd_polint2 a_privat_sd_polint2 a_paper_sd_polint2 a_bild_sd_polint2 a_paper_and_sd_polint2 ///
	a_magazine_sd_polint2 a_polinter_sd_polint2 a_talk_sd_polint2 a_countcontact_sd_polint2 N) ///
	label title("Lineare Regressionsmodelle 2009") ///
	compress

esttab a_oeff_pidno1 a_privat_pidno1 a_paper_pidno1 a_bild_pidno1 a_paper_and_pidno1 a_magazine_pidno1 a_polinter_pidno1 a_talk_pidno1 a_countcontact_pidno1 ///
	using "TablesII/nice table.rtf", append ///
	b(2) not nonumb stats(a_oeff_sd_pidno1 a_privat_sd_pidno1 a_paper_sd_pidno1 a_bild_sd_pidno1 a_paper_and_sd_pidno1 ///
	a_magazine_sd_pidno1 a_polinter_sd_pidno1 a_talk_sd_pidno1 a_countcontact_sd_pidno1 N) ///
	compress
	
esttab a_oeff_indiff12_h a_privat_indiff12_h a_paper_indiff12_h a_bild_indiff12_h a_paper_and_indiff12_h a_magazine_indiff12_h a_polinter_indiff12_h a_talk_indiff12_h a_countcontact_indiff12_h ///
	using "TablesII/nice table.rtf", append ///
	b(2) not nonumb stats(a_oeff_sd_indiff12_h a_privat_sd_indiff12_h a_paper_sd_indiff12_h a_bild_sd_indiff12_h a_paper_and_sd_indiff12_h ///
	a_magazine_sd_indiff12_h a_polinter_sd_indiff12_h a_talk_sd_indiff12_h a_countcontact_sd_indiff12_h N) ///
	compress

esttab a_oeff_cons a_privat_cons a_paper_cons a_bild_cons a_paper_and_cons a_magazine_cons a_polinter_cons a_talk_cons a_countcontact_cons ///
	using "TablesII/nice table.rtf", append ///
	b(2) not nonumb stats(a_oeff_sd_cons a_privat_sd_cons a_paper_sd_cons a_bild_sd_cons a_paper_and_sd_cons ///
	a_magazine_sd_cons a_polinter_sd_cons a_talk_sd_cons a_countcontact_sd_cons N) ///
	compress
	
* 2013
esttab b_oeff_polint2 b_privat_polint2 b_paper_polint2 b_bild_polint2 b_paper_and_polint2 b_magazine_polint2 b_polinter_polint2 b_talk_polint2 b_countcontact_polint2 ///
	using "TablesII/nice table.rtf", append ///
	b(2) not nonumb stats(b_oeff_sd_polint2 b_privat_sd_polint2 b_paper_sd_polint2 b_bild_sd_polint2 b_paper_and_sd_polint2 ///
	b_magazine_sd_polint2 b_polinter_sd_polint2 b_talk_sd_polint2 b_countcontact_sd_polint2 N) ///
	label title("Lineare Regressionsmodelle 2013") ///
	compress

esttab b_oeff_pidno1 b_privat_pidno1 b_paper_pidno1 b_bild_pidno1 b_paper_and_pidno1 b_magazine_pidno1 b_polinter_pidno1 b_talk_pidno1 b_countcontact_pidno1 ///
	using "TablesII/nice table.rtf", append ///
	b(2) not nonumb stats(b_oeff_sd_pidno1 b_privat_sd_pidno1 b_paper_sd_pidno1 b_bild_sd_pidno1 b_paper_and_sd_pidno1 ///
	b_magazine_sd_pidno1 b_polinter_sd_pidno1 b_talk_sd_pidno1 b_countcontact_sd_pidno1 N) ///
	compress
	
esttab b_oeff_indiff12_h b_privat_indiff12_h b_paper_indiff12_h b_bild_indiff12_h b_paper_and_indiff12_h b_magazine_indiff12_h b_polinter_indiff12_h b_talk_indiff12_h b_countcontact_indiff12_h ///
	using "TablesII/nice table.rtf", append ///
	b(2) not nonumb stats(b_oeff_sd_indiff12_h b_privat_sd_indiff12_h b_paper_sd_indiff12_h b_bild_sd_indiff12_h b_paper_and_sd_indiff12_h ///
	b_magazine_sd_indiff12_h b_polinter_sd_indiff12_h b_talk_sd_indiff12_h b_countcontact_sd_indiff12_h N) ///
	compress
	
esttab b_oeff_cons b_privat_cons b_paper_cons b_bild_cons b_paper_and_cons b_magazine_cons b_polinter_cons b_talk_cons b_countcontact_cons ///
	using "TablesII/nice table.rtf", append ///
	b(2) not nonumb stats(b_oeff_sd_cons b_privat_sd_cons b_paper_sd_cons b_bild_sd_cons b_paper_and_sd_cons ///
	b_magazine_sd_cons b_polinter_sd_cons b_talk_sd_cons b_countcontact_sd_cons N) ///
	compress
	
	
*****************************************************
***Table 6
*****************************************************
	
tab a_duell6 [aw=a_weight]
tab b_duell5 [aw=b_weight]
tab b_arena_merk6 [aw=b_weight]
tab b_arena_stein6 [aw=b_weight]
tab b_wahlomat5 [aw=b_weight]
tab b_wahlomat6 [aw=b_weight]
tab b_wahlomat7 [aw=b_weight]

gen a_pidyes1 = (a_pidno1-1)*(-1)
gen b_pidyes1 = (b_pidno1-1)*(-1)


logit a_duell6 a_polint2 a_pidyes1 a_indiff12_h [pw=a_weight]
margins, dydx(a_polint2 a_pidyes1 a_indiff12_h)

foreach var in b_duell5 b_arena_merk6 b_arena_stein6 b_wahlomat6 {
logit `var' b_polint2 b_pidyes1 b_indiff12_h  [pw=b_weight]
margins, dydx(b_polint2 b_pidyes1 b_indiff12_h)
}






save "${data}\data2009_2013 participants 7 waves.dta", replace


